from tqdm import tqdm
from .base import Tool

TOOL = Tool(
    name="read_file",
    description="读取指定文件的内容",
    parameters={
        "path": {
            "type": "string",
            "description": "要读取的文件路径",
            "required": True
        }
    },
    example='''<read_file>
<path>example.txt</path>
</read_file>'''
)

def execute(params: dict) -> str:
    """执行read_file工具"""
    with tqdm(total=100, desc="执行 read_file", ncols=100) as pbar:
        try:
            path = params.get("path")
            if not path:
                return "错误：未提供文件路径"
            
            pbar.update(50)
            with open(path, "r", encoding="utf-8") as f:
                content = f.read()
            pbar.update(50)
            return content
            
        except Exception as e:
            return f"读取文件时出错：{str(e)}"